package com.boss.salary.service.impl;

import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.IPersonTypeSalaryDao;
import com.boss.salary.entity.PersonTypeSalary;
import com.boss.salary.service.IPersonTypeSalaryService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PersonTypeSalaryServiceImpl implements IPersonTypeSalaryService {

    @Autowired
    private IPersonTypeSalaryDao personTypeSalaryDao;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePersonTypeSalary(PersonTypeSalary personTypeSalary, UserDTO userDTO) {
        personTypeSalary.setMofDivCode(userDTO.getProvince());
        personTypeSalaryDao.savePersonTypeSalary(personTypeSalary);
    }

    @Override
    public void updatePersonTypeSalary(PersonTypeSalary personTypeSalary, UserDTO userDTO) {

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSavePersonTypeSalary(List<PersonTypeSalary> personTypes, UserDTO userDTO)  throws Exception  {
        Map<String, Object> queryParam=new HashMap<>();
        queryParam.put("mof_div_code",userDTO.getProvince());
        if(!CollectionUtils.isEmpty(personTypes)) {
            queryParam.put("personTypeCode",personTypes.get(0).getPersonTypeCode());
        }else{
            throw new Exception("未识别到人员类型，无法清除挂接工资项信息");
        }
        personTypeSalaryDao.physicsDeletePersonTypeSalary(queryParam);
        personTypeSalaryDao.batchSavePersonTypeSalary(personTypes,userDTO.getProvince());

    }

    @Override
    public List<PersonTypeSalary> getInfoByPersonTypes(List<String> personTypes, UserDTO userDTO) {
        return personTypeSalaryDao.getInfoByPersonTypes(personTypes,userDTO.getProvince());
    }


    @Override
    public List<Map<String, Object>> getAllPersonTypeByProvince(UserDTO userDTO) {
        return personTypeSalaryDao.getAllPersonTypeByProvince(userDTO.getProvince());
    }

    @Override
    public List<Map<String, Object>> getAllPersonTypeSalary(UserDTO userDTO) {
        List<Map<String, Object>> re=new ArrayList<>();
        //获取所有工资类型
        List<Map<String, Object>> personTypeMaps= personTypeSalaryDao.getAllPersonTypeByProvince(userDTO.getProvince());
        if(!CollectionUtils.isEmpty(personTypeMaps)){
            List<String> personTypes=new ArrayList<>();
            for(Map<String, Object> map:personTypeMaps){
                personTypes.add(map.get("personTypeCode").toString());//将所有工资类型code遍历
                Map<String, Object> rmap=new HashMap<>();
                rmap.put("meritPayCode",map.get("personTypeCode").toString());
                rmap.put("meritPayName",map.get("personTypeName").toString());
                rmap.put("salaryCodes",new ArrayList<>());
                List<String> columnNames=new ArrayList<>();
                columnNames.add("s1");
                rmap.put("columnNames",columnNames);
                rmap.put("percent","");
                re.add(rmap);
            }
            //查出所有工资类型与工资项绑定
            List<PersonTypeSalary> personTypeSalarys= personTypeSalaryDao.getInfoByPersonTypes(personTypes,userDTO.getProvince());
            for(Map<String, Object> map:re) {
                for (PersonTypeSalary p : personTypeSalarys) {
                   if(p.getPersonTypeCode().equals(map.get("meritPayCode"))) {
                       List l=(ArrayList)map.get("salaryCodes");
                       Map<String,String> salaryMap=new HashMap<>();
                       if(!"s2".equals(p.getColumnName())&& SystemDictConstant.IS_TRUE.equals(p.getIsShow())) {
                           salaryMap.put("salaryCode", p.getColumnName());
                           salaryMap.put("salaryName", p.getSalaryName());
                           l.add(salaryMap);
                       }
                   }
                }
            }
        }
        return re;
    }
}
